﻿using System;
using System.Data;
using System.Collections.Generic;
using Slja.Model;
using System.Text.RegularExpressions;
using Slja.DAL;
namespace Slja.BLL
{
    /// <summary>
    /// ClientModel
    /// </summary>
    public partial class ClientBLL
    {
        private readonly Slja.DAL.ClientDAL dal = new Slja.DAL.ClientDAL();
        public ClientBLL()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ClientName)
        {
            return dal.Exists(ClientName);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Slja.Model.ClientModel model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Slja.Model.ClientModel model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ClientName)
        {

            return dal.Delete(ClientName);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string ClientNamelist)
        {
            return dal.DeleteList(ClientNamelist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.Model.ClientModel GetModel(string ClientName)
        {

            return dal.GetModel(ClientName);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Slja.Model.ClientModel> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        public string GetClientFromKeyWord(string keyword,string clientClass)
        {
            string strXml = "<all/>";
            if (keyword.Length >= 2 || keyword.Length ==0)
            {
                Regex regChz2 = new Regex(@"[\u0391-\uFFE5]{2,}");//2个以上的汉字
                Regex regEng2 = new Regex(@"^[A-Z,a-z]{2,}$");//2个以上的拼音字母
                string strWhere = "1=1 ";
                if (string.IsNullOrEmpty(clientClass))
                {
                    strWhere += " and ClientType='1'";
                }
                else
                {
                    strWhere += " and ClientType='" + clientClass + "'";
                }
                if (regChz2.IsMatch(keyword))
                {
                    strWhere += " and ClientName like '" + keyword + "%'";
                }
                if (regEng2.IsMatch(keyword))
                {
                    strWhere += "  and (Pinyin like '" + keyword + "%' OR PyShort like '" + keyword + "%')";
                }

                //ClientDAL dal = new RegionCity();
                DataSet ds = dal.GetList(strWhere);
                if (ds.Tables.Count > 0)
                {
                    strXml = BLLCommon.BuildXmlData(ds.Tables[0], "ClientID", "ClientName", "{0}*{1}", "{1}");
                }
            }
            return strXml;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Slja.Model.ClientModel> DataTableToList(DataTable dt)
        {
            List<Slja.Model.ClientModel> modelList = new List<Slja.Model.ClientModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Slja.Model.ClientModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Slja.Model.ClientModel();
                    dal.AssignModel(dt.Rows[n], model);                   
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

